<div class="wrapper">
    <h1>用户列表：</h1>
    <table cellpadding="0" cellspacing="0" border="0" width="500" >
        <thead>
            <tr>
                <th width="20%">用户ID</th>
                <th width="50%">用户名称</th>
                <th width="30%">操作</th>
            </tr>            
        </thead>
        <tbody>
            <?php if(!empty($user)):?>
                <?php foreach($user as $data):?>
                    <tr>
                        <td><?php echo $data['uid'];?></td>
                        <td><?php echo $data['username'];?></td>
                        <td><a href="javascript:void(0)" class="a_btn <?php if($data['uid'] == $loginUid):?>edit_user<?php endif;?>">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" class="a_btn del_user">删除</a></td>
                    </tr>
                <?php endforeach;?>
            <?php endif;?>
        </tbody>
    </table>
    <h1>添加新用户：</h1>
    <ul>
        <li><span class="block">用户名：</span><input type="text" name="username" class="ipt_blank_min" value="" /></li>
        <li><span class="block">密码：</span><input type="password" name="password" class="ipt_blank_min" value="" /></li>
        <li><span class="block">确认密码：</span><input type="password" name="confirm_password" class="ipt_blank_min" value="" /></li>
        <li><a href="javascript:void(0)" class="a_btn add_user">添加</a><a class="red alter"></a></li>
    </ul>
</div>
<script type="text/javascript">
$(function(){
    //添加用户
    $(".add_user").click(function(){
        var username = $("input[name=username]").val();
        var password = $("input[name=password]").val();
        var confirm_password = $("input[name=confirm_password]").val();
        
        if(username.length == 0 || username == null || username == '' || password.length == 0 || password == null || password == ''){
            _alert("用户名或密码不能为空");
            return false;
        }
        
        if(password != confirm_password){
            _alert("密码不一致");
            return false;
        }
        
        var url = "/index.php?c=admin&m=addUser";
        var data = {username:username,password:password};
        var res = _ajax(url,data);
        _ajax_alert(res);
    });
    
    //删除用户
    $(".del_user").click(function(){
        var a = confirm("确认删除？");
        if(a){
            var uid = $(this).parent("td").siblings("td:eq(0)").text();
            var url = "/index.php?c=admin&m=delUser";
            var data = {uid:uid};
            var res = _ajax(url,data);
            alert(res.info);
            window.location.reload();
            return true;
        }
        return false;
    });
    
    //编辑用户
    $(".edit_user").click(function(){
        var uid = $(this).parent("td").siblings("td:eq(0)").text();
        var username = $(this).parent("td").siblings("td:eq(1)").text();
        var html = "";
        html += "<ul>";
        html += "<li><span>用户名：</span>"+username+"</li>";
        html += "<li><span>新密码：</span><input type='password' name='update_password' class='ipt_blank_min' value='' /></li>";
        html += "<li><span>确认密码：</span><input type='password' name='update_confirm_password' class='ipt_blank_min' value='' /></li>";
        html += "<li><a href='javascript:void(0)' class='a_btn update_user' uid='"+uid+"'>更新</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick='return _cancel_edit();' class='a_btn'>取消</a><a class='red edit_alter'></a></li>";
        html += "</ul>";
        _show_edit_bg();
        _show_edit(html);
        return true;
    });
    
    //更新密码
    $(".update_user").live("click",function(){
        var uid = $(this).attr("uid");
        var password = $('input[name=update_password]').val();
        var confirm_password = $('input[name=update_confirm_password]').val();
        
        if(uid == '' || uid.length == 0 || uid ==null){
             _edit_alert("网络异常，请刷新后再更新信息");
             return false;
        }
        
        if(password.length == 0 || password == null || password == ''){
            _edit_alert("新密码不能为空");
            return false;
        }
        
        if(password != confirm_password){
            _edit_alert("密码不一致");
            return false;
        }
        
        var url = "/index.php?c=admin&m=updateUser";
        var data = {uid:uid, password:password};
        var res = _ajax(url,data);
        _ajax_edit_alert(res);
    });
})
</script>